using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Work.API.Applications.Read.Commands;
using Work.API.Applications.Write.Commands;
using Work.ErrorCode;

namespace Work.API.Controllers
{
    /// <summary>
    /// 门诊结账控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutpatientSettlementController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<OutpatientSettlementController> _logger;

        public OutpatientSettlementController(IMediator mediator, ILogger<OutpatientSettlementController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取未支付患者列表
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<System.Collections.Generic.List<Work.API.Dtos.UnpaidPatientDto>>> GetUnpaidPatients(
            string? registrationNum = null,
            string? idCard = null,
            string? patientName = null)
        {
            try
            {
                var query = new GetUnpaidPatientsQuery
                {
                    RegistrationNum = registrationNum,
                    IdCard = idCard,
                    PatientName = patientName
                };
                return await _mediator.Send(query);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "查询未支付患者失败");
                return new ApiResult<System.Collections.Generic.List<Work.API.Dtos.UnpaidPatientDto>>
                {
                    Code = ApiEnum.失败,
                    Message = $"查询失败：{ex.Message}",
                    Data = new System.Collections.Generic.List<Work.API.Dtos.UnpaidPatientDto>()
                };
            }
        }

        /// <summary>
        /// 获取患者账单信息
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<Work.API.Dtos.PatientBillDto>> GetPatientBill(
            int? appointmentId = null,
            string? registrationNum = null,
            string? idCard = null)
        {
            try
            {
                var query = new GetPatientBillQuery
                {
                    AppointmentId = appointmentId,
                    RegistrationNum = registrationNum,
                    IdCard = idCard
                };
                return await _mediator.Send(query);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "查询患者账单失败");
                return new ApiResult<Work.API.Dtos.PatientBillDto>
                {
                    Code = ApiEnum.失败,
                    Message = $"查询失败：{ex.Message}",
                    Data = null!
                };
            }
        }

        /// <summary>
        /// 核算总金额
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<Work.API.Dtos.SettlementSummaryDto>> CalculateTotalAmount(int appointmentId)
        {
            try
            {
                var query = new CalculateTotalAmountQuery
                {
                    AppointmentId = appointmentId
                };
                return await _mediator.Send(query);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "核算总金额失败");
                return new ApiResult<Work.API.Dtos.SettlementSummaryDto>
                {
                    Code = ApiEnum.失败,
                    Message = $"核算失败：{ex.Message}",
                    Data = null!
                };
            }
        }

        /// <summary>
        /// 结算支付
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> Settle(SettlePaymentCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "结算支付失败");
                return new ApiResult<int>
                {
                    Code = ApiEnum.失败,
                    Message = $"结算失败：{ex.Message}",
                    Data = 0
                };
            }
        }
    }
}

